package com.jb.repair.acticity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jb.repair.R;
import com.jb.repair.common.AppConfig;
import com.jb.repair.dao.Db;
import com.jb.repair.model.SbbdzdyModel;
import com.jb.repair.model.TtjlModel;
import com.jb.repair.util.DownAttachmentDialogUtil;
import com.jb.repair.util.DownFileAsyncTask;
import com.jb.repair.util.IntentUtils;
import com.jb.repair.util.StringUtils;
import com.jb.repair.view.InfoTextView;
import com.jb.repair.view.TitleView;

import java.io.File;
import java.util.List;

/*
投退记录详情页
 */
public class BeRecordActivtity extends BaseActivity {

    private TitleView titleView;
    private InfoTextView tv_end_time, tv_note, tv_begin_time, tv_function_range, tv_function_descri,
            tv_be_report, tv_boolean;
    private LinearLayout ll_ttjl;
    private Db db;
    private TtjlModel model;
    private int i = -1;

    private DownAttachmentDialogUtil dialogUtil;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ttjlxq);
        initviews();
        db = Db.getInstance(this);
        String form_CODE = db.getName2("CODE", "TB_SYS_CODE", "CODE_NAME", "投退记录");
        Log.e("1231", form_CODE);

        model = (TtjlModel) getIntent().getSerializableExtra("ttjl");
        String id_zhuanye = getIntent().getStringExtra("id_zhuanye");

//        int j = model.tcsj.lastIndexOf(".");
//        if (-1 != j){
//            tv_end_time.setText(model.tcsj.substring(0,j));
//        }else{
        tv_end_time.setText(model.exit_time);
//        }

//        int i = model.trsj.lastIndexOf(".");
//        if (-1 != i){
//            tv_begin_time.setText(model.trsj.substring(0,i));
//        }else{
        tv_begin_time.setText(model.startup_time);
//        }
        tv_function_range.setText(model.exit_function);
        tv_function_descri.setText(model.exit_describe);
        Log.e("tv_attachment", model.attachment);

        String name = db.getName("WJMC", "TB_FILE_LB", "GUID", model.attachment);
        if (!StringUtils.isNullOrEmpty(name)) {
            tv_be_report.setText(name);
            i = 1;
        } else {
            String name1 = db.getName("WJMC", "TB_FILE_LB", "OBJ_ID", model.attachment);
            tv_be_report.setText(name1);
            i = 2;
        }

        String visible = db.getVisible(form_CODE, "is_fault_exit_editable");
        if (!visible.equals("T")) {
            tv_boolean.setVisibility(View.GONE);
        }
        tv_boolean.setText(model.is_fault_exit_editable);
        tv_note.setText(model.note);

        //----------------------------------------------处理自定义字段逻辑-----------------------------------------------------------
        //查找自定义字段显示
        //拿到表单编码
        List<SbbdzdyModel> sbbdzdyModelList = db.getSbbdzdyModel(form_CODE, id_zhuanye);
        for (SbbdzdyModel model1 : sbbdzdyModelList) {
            if (model1.is_show_eqbook.equals("T")) {
                InfoTextView infoTextView = new InfoTextView(this);
                ll_ttjl.addView(infoTextView);
                //需要设置key的值
                infoTextView.setKey(model1.cus_name);

                //设置value的值
                String format = model1.field_format;
                String value = db.getZdyzdValue(model1.guid, model.guid);
                if (value == null) {
                    value = "";
                } else if (format != null && "时间".equals(format) && value.length() > 10) {
                    value = value.substring(0, 10);
                }
                infoTextView.setText(value);
//                String zdyzdValue = db.getZdyzdValue(model1.guid, model.guid);
//                infoTextView.setText(zdyzdValue);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = (int) getResources().getDimension(R.dimen.ledger_info_textsize);
                infoTextView.setLayoutParams(params);
            }

        }
//-------------------------------------------------------------------------------------------------------
    }

    private void initviews() {
        titleView = (TitleView) this.findViewById(R.id.titleView);

        tv_end_time = (InfoTextView) findViewById(R.id.tv_end_time);
        tv_begin_time = (InfoTextView) findViewById(R.id.tv_begin_time);
        tv_function_range = (InfoTextView) findViewById(R.id.tv_function_range);
        tv_function_descri = (InfoTextView) findViewById(R.id.tv_function_descri);
        tv_be_report = (InfoTextView) findViewById(R.id.tv_be_report);
        tv_boolean = (InfoTextView) findViewById(R.id.tv_boolean);
        tv_note = (InfoTextView) findViewById(R.id.tv_note);
        ll_ttjl = (LinearLayout) findViewById(R.id.ll_ttjl);

        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener() {
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                finish();
            }
        });

        tv_be_report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("点击测试", "收到点击事件");
                //附件的guid
                final String guid;
                //附件的文件类型
                final String fjlx;
                if (i == 1) {
                    guid = db.getName("GUID", "TB_FILE_LB", "GUID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "GUID", model.attachment);
                } else {
                    guid = db.getName("GUID", "TB_FILE_LB", "OBJ_ID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "OBJ_ID", model.attachment);
                }
                //判断是否有附件
                boolean nullOrEmpty = StringUtils.isNullOrEmpty(tv_be_report.getText());
                Log.e("LLL", String.valueOf(nullOrEmpty));
                if (nullOrEmpty) {
                    return;
                } else {
                    //有附件
                    String filePath = AppConfig.ATTACHMENT_PATh + guid + "." + fjlx;
                    Log.e("1111", filePath);
                    File file = new File(filePath);
                    if (!file.exists()) {
                        Toast.makeText(BeRecordActivtity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                        dialogUtil = new DownAttachmentDialogUtil(BeRecordActivtity.this, R.style.myDialog,
                                new DownAttachmentDialogUtil.AttachDialogListener() {
                                    @Override
                                    public void onClick(View v) {
                                        switch (v.getId()) {
                                            case R.id.attach_dialog_canlebtn:
                                                dialogUtil.dismiss();
                                                break;
                                            case R.id.attach_dialog_surebtn:
                                                new DownFileAsyncTask(BeRecordActivtity.this, guid, model.attachment, fjlx).execute();
                                                dialogUtil.dismiss();

                                                break;
                                        }
                                    }

                                });
                        Window win = dialogUtil.getWindow();
                        win.setGravity(Gravity.CENTER);
                        dialogUtil.show();
                    } else {
                        IntentUtils.JumpToIntent(BeRecordActivtity.this, fjlx, filePath);

                    }

                }
            }


        });

    }
}